package com.jackchong.base;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by lyf on 2019/2/26.
 */

@SuppressLint("ValidFragment")
public class SimpleDialogFragment extends BaseDialogFragment {

    private int mLayoutRes;
    private OnInitListener mOnInitListener;


    @SuppressLint("ValidFragment")
    public SimpleDialogFragment(BaseToolsActivity activity, int layoutRes) {
        mActivity = activity;
        mLayoutRes = layoutRes;
    }

    @Override
    public int getContentLayout() {
        return mLayoutRes;
    }

    @Override
    public void init(View view) {

        view.setOnClickListener(v -> dismiss());
        ((ViewGroup) view).getChildAt(0).setOnClickListener(v -> {});

        if (mOnInitListener != null){
            mOnInitListener.init(view);
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);

        mActivity.hideInputKeyVisibility();
        new Handler().postDelayed(() -> {
            if (mActivity.getInputKeyVisibility()) {
                mActivity.hideInputKeyVisibility();
            }
        }, 200);
    }

    public void show() {
        super.show(mActivity.getFragmentManager(), "");
    }

    public interface OnInitListener{
        void init(View view);
    }

    public void setOnInitListener(OnInitListener onInitListener) {
        mOnInitListener = onInitListener;
    }
}
